package org.itboys.core.service.mysql.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.entity.mysql.SundayFreight;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/20
 * Description:
 */

@Service
public class SundayFreightService extends BaseMySqlService<SundayFreight> {

    /**
     * 新增或保存运费设置
     *
     * @param freight
     * @throws Exception
     */
    @Transactional
    public void save(Long id, SundayFreight freight) throws Exception {
        freight.setId(id);
        if (LongUtil.isNotZreo(id)) {
            super.update(freight);
        } else {
            if (super.count(new HashMap<>()) > 0) {
                throw new ErrorException("");
            } else super.insert(freight);
        }
    }

    /**
     * 查询运费设置
     *
     * @param request
     * @return
     */
    public PageResult<SundayFreight> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        return new PageResult<>(super.select(param), super.count(param));
    }

    /**
     * 查找单个运费记录
     *
     * @param id
     * @return
     */
    public SundayFreight findOne(Long id) {
        SundayFreight freight = null;
        if (LongUtil.isNotZreo(id)) {
            freight = super.find(id);
        } else {
            freight = new SundayFreight();
        }

        return freight;
    }

}
